package system

import (
	"context"
	"fmt"
	"private-domain-overseas-service/admin/business/api/internal/repository/system"
	"private-domain-overseas-service/pkg/constant"
	"private-domain-overseas-service/pkg/xerr"
	"private-domain-overseas-service/tools/ent"
	"time"

	"private-domain-overseas-service/admin/business/api/internal/svc"
	"private-domain-overseas-service/admin/business/api/internal/types"

	"github.com/zeromicro/go-zero/core/logx"
)

type SystemConfigDeleteLogic struct {
	logx.Logger
	ctx    context.Context
	svcCtx *svc.ServiceContext
}

// NewSystemConfigDeleteLogic - 系统配置删除
func NewSystemConfigDeleteLogic(ctx context.Context, svcCtx *svc.ServiceContext) *SystemConfigDeleteLogic {
	return &SystemConfigDeleteLogic{
		Logger: logx.WithContext(ctx),
		ctx:    ctx,
		svcCtx: svcCtx,
	}
}

func (l *SystemConfigDeleteLogic) SystemConfigDelete(req *types.SystemConfigDeleteRequest) error {
	// 创建实例
	configDao := system.NewSystemConfigDao(l.ctx, l.svcCtx)

	// 组装参数
	configInfo := &ent.HzSystemConfig{
		ID:       int(req.Id),
		DeleteAt: time.Now().Unix(),
	}

	// mysql软删除系统配置信息
	err := configDao.SystemConfigDelete(configInfo, nil)
	if err != nil {
		return xerr.ErrCodeMsg(xerr.ServerOperationExceptionError, fmt.Sprintf("mysql删除系统配置信息失败：%s", err.Error()))
	}

	// redis删除系统配置信息
	_, err = l.svcCtx.BizRedis.DelCtx(l.ctx, constant.HzAdminSystemConfigKey+req.SysteKey)
	if err != nil {
		return xerr.ErrCodeMsg(xerr.ServerOperationExceptionError, fmt.Sprintf("redis删除系统配置信息失败：%v", err.Error()))
	}

	return nil
}
